﻿Imports System.Activities
Imports System.Data.SqlClient
Imports System.Activities.Presentation.Metadata
Imports System.ComponentModel
Imports System.Drawing

Public Class UpdateStatisticActiviy
    Inherits MSSQLBaseActivity

    Public Property TableName As InArgument(Of String)
    Public Property IndexName As InArgument(Of String)

    Public Sub New()
        Me.DisplayName = My.Resources.UpdateStatisticActivityDisplayName
    End Sub

    Protected Overrides Function BeginExecute(context As System.Activities.AsyncCodeActivityContext, callback As System.AsyncCallback, state As Object) As System.IAsyncResult
        Dim tableName = context.GetValue(Me.TableName)
        Dim indexName = context.GetValue(Me.IndexName)
        If indexName Is Nothing Then indexName = ""
        Dim conn = context.GetValue(Me.ConnectionStrings)
        Dim useLog = context.GetValue(Me.OutSQLLog)
        Dim asyncExecute = New asyncExecuteDelegate(AddressOf ExecuteSQL)
        context.UserState = asyncExecute

        Return asyncExecute.BeginInvoke(CreateSQLStrings(tableName, indexName), conn, False, useLog, callback, state)
    End Function

    Public Overrides Sub Register()
        MyBase.Register()

        Dim build As New AttributeTableBuilder
        'ReCreateIndexActivity Activity
        build.AddCustomAttributes(GetType(UpdateStatisticActiviy),
                                              New ToolboxBitmapAttribute(GetType(UpdateStatisticActiviy), "UpdateStatistics.png"))
        build.AddCustomAttributes(GetType(UpdateStatisticActiviy), "TableName",
                                              New RequiredArgumentAttribute,
                                              New CategoryAttribute(My.Resources.CategoryDetail),
                                              New DisplayNameAttribute(My.Resources.TableNameDisplay),
                                              New DescriptionAttribute(My.Resources.TableNameDescription))
        build.AddCustomAttributes(GetType(UpdateStatisticActiviy), "IndexName",
                                              New CategoryAttribute(My.Resources.CategoryDetail),
                                              New DisplayNameAttribute(My.Resources.IndexNameDisplay),
                                              New DescriptionAttribute(My.Resources.IndexNameDescription))

        MetadataStore.AddAttributeTable(build.CreateTable)
    End Sub

    Protected Overrides Function ExecuteSQL(script As String, connect As String, needRes As Boolean, ByVal needLog As Boolean) As System.Data.DataSet
        Dim sqlExec As New SQLExecute With {.Timeout = 0}
        sqlExec.ExecuteSQL(script, connect, needRes)

        Return sqlExec.ConvertMessageToDataset(sqlExec.InfoMessages)
    End Function

    ''' <summary>実行する SQL の生成</summary>
    ''' <remarks>http://msdn.microsoft.com/ja-jp/library/ms187348(SQL.90).aspx</remarks>
    Private Function CreateSQLStrings(ByVal tableName As String, ByVal indexName As String) As String
        Dim sqlBuilder As New System.Text.StringBuilder
        With sqlBuilder
            .Append("UPDATE STATISTICS ")
            .Append(tableName + " ")
            If indexName.Trim <> "" Then
                .Append(indexName + " ")
            End If
            .Append(";")
        End With
        Return sqlBuilder.ToString
    End Function
End Class
